﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using System.ComponentModel;
using System.Windows.Threading;
using System.Windows.Media;

namespace MS.Common
{
    /// <summary>
    ///
    /// </summary>
    public class ModalWindow : Window
    {

        /// <summary>
        /// 切换窗体样式
        /// 多线程是为了防止闪动现象
        /// </summary>
        public BackgroundWorker bwChangeStyle = null;

        /// <summary>
        /// 当前窗体
        /// </summary>
        public string NowWindow;

        public ModalWindow()
        {
           
        }
        /// <summary>
        /// 关闭窗体前，移除蒙版
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosing(CancelEventArgs e)
        {
            if (this.Owner != null)
            {
                //容器Grid
                Grid grid = this.Owner.Content as Grid;
                //父级窗体原来的内容
                UIElement original = VisualTreeHelper.GetChild(grid, 0) as UIElement;
                //将父级窗体原来的内容在容器Grid中移除
                grid.Children.Remove(original);
                //赋给父级窗体
                this.Owner.Content = original;
            }
            VisualStateManager.GoToState(this, "Closed", true);
        }


        /// <summary>
        /// 按照 Tab 键顺序将焦点移至下一个可获得焦点的元素
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnMoveNextFocus(KeyEventArgs e)
        {
            // MoveFocus takes a TraveralReqest as its argument.
            TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);
            // Gets the element with keyboard focus.
            UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;
            // Change keyboard focus. 
            if (elementWithFocus != null)
            {
                elementWithFocus.MoveFocus(request);
            }
            e.Handled = true;
        }
    }

    public static class ModalHelper 
    {
        /// <summary>
        /// 蒙版在父窗体
        /// </summary>
        /// <param name="owner">在哪个窗体基础上弹出</param>
        /// <param name="Uc">弹出的内容</param>
        /// <returns></returns>
        public static MessageBoxResult Show(Window owner, UserControl Uc,string Title,double width,double height)
        {
            ModalWindow messageBox = new ModalWindow();
            if (owner != null)
            {
                //蒙板
                Grid layer = new Grid() { Background = new SolidColorBrush(Color.FromArgb(128, 0, 0, 0)) };
                //父级窗体原来的内容
                UIElement original = owner.Content as UIElement;
                owner.Content = null;
                //容器Grid
                Grid container = new Grid();
                container.Children.Add(original);//放入原来的内容
                container.Children.Add(layer);//在上面放一层蒙板                             
                owner.Content = container; //将装有原来内容和蒙板的容器赋给父级窗体
                messageBox.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            }
            else
            {
                messageBox.ShowInTaskbar = true;
            }

            messageBox.Owner = owner;
            messageBox.Title = Title;
            messageBox.Width = width;
            messageBox.Height = height;
            messageBox.Content = Uc;
            Uc.Width = width;
            Uc.Height = height;
            //该行是关键代码，窗体显示后进入阻塞状态，直到窗体有返回值：即点击了某个按钮或者关闭了窗体
            bool? result = messageBox.ShowDialog();
            return MessageBoxResult.OK;
        }

        /// <summary>
        /// 蒙版在当前弹出窗体
        /// </summary>
        /// <param name="owner">在哪个窗体基础上弹出</param>
        /// <param name="Uc">弹出的内容</param>
        /// <returns></returns>
        public static MessageBoxResult ShowMDiag(Window owner, UserControl Uc, string Title, double width, double height)
        {
            MDiagWindow messageBox = new MDiagWindow();
            if (owner != null)
            messageBox.Owner = owner;
            messageBox.Title = Title;
            Uc.Width = width;
            Uc.Height = height;
            messageBox.Width = width;
           
            messageBox.Height = height;
            messageBox.mycont.Content = Uc;
            //该行是关键代码，窗体显示后进入阻塞状态，直到窗体有返回值：即点击了某个按钮或者关闭了窗体
            bool? result = messageBox.ShowDialog();
            return MessageBoxResult.OK;
        }
    }

}
